<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/8/24
 * Time: 16:05
 */
namespace app\withdraw;


use app\common\Common;
use app\data\ShopData;
use app\data\UserData;
use think\Db;

class WithdrawReview extends Base
{
    /**
     * 进行审核
     * @param $order_num
     * @param $status
     * @param string $msg
     * @return \think\response\Json
     */
    public function withdrawReview($order_num, $status, $msg = '审核通过')
    {
        if ($status != 1 && $status != 2) {
            return json(['code' => $this->f_code, 'msg' => '审核状态错误']);
        }
        $type_status = Common::getTableValue('drh_withdraw', 'review_type', 'order_num', $order_num);
        if (empty($type_status)) {
            return json(['code' => $this->f_code, 'msg' => '不存在改提现请求']);
        }
        if (strlen($msg) > 200) {
            return json(['code' => $this->f_code, 'msg' => '驳回信息过长 请限制在200字内']);
        }
        $time = time();
        $user = new UserData();
        $time_start = strtotime(date('Y-m-d', $time - 24 * 60 * 60 * 6));//7天前的
        try {
            if ($status == 2) {//审核失败  数据要回滚

                if ($type_status == 2) {//用户回滚
                    $this->rollBackUserWithdraw($order_num, $time);
                }else if($type_status ==1){//商户回滚
                    $this->rollBackShopWithdraw($order_num, $time);
                }else{
                    return json(['code' => $this->f_code, 'msg' => '非法操作1']);
                }
            }
            $res = $user->withdrawReview($order_num, $status, $msg, $time, $time_start);//提现完结
            if ($res == $this->f_code) {
                Db::rollback();
                return json(['code' => $this->f_code, 'msg' => '非法操作2']);
            }
            return json(['code' => $this->t_code, 'msg' => 'OK']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => $e->getMessage()]);
        }
    }

    private function rollBackUserWithdraw($order_num, $time)
    {
        $user = new UserData();
        $user_info = $user->getWithdrawUserInfo($order_num);
        $user_integral = $user_info['user_integral'];//用户初始积分
        $user_money = $user_info['user_money'];//用户初始金钱 f
        $money = $user_info['total_money'];//退回的金额
        $fees = 0;
        $user->addUserFlow($user_info['user_id'], $user_integral, $user_money, $time, '+' . $money, 0,
            $order_num, 3, $user_money - $money, $user_integral, $fees);
        $user->changIntegralMoney($user_info['user_id'], $user_integral, $user_money + $money);
    }

    /**
     * 回滚商户数据
     * @param $order_num
     * @param $time
     */
    private function rollBackShopWithdraw($order_num, $time)
    {
        $shop=new ShopData();
        $shopInfo=$shop->getWithdrawUserInfo($order_num);
        $fees = 0;
        $money = $shopInfo['total_money'];//退回的金额
        $shop_money = $shopInfo['money'];//商户初始金钱
        $shop_id=$shopInfo['id'];
        $shop_integral = $shopInfo['integral'];//商户初始积分
        $shop->shopFlow($order_num,$time,$shopInfo,3,$money,0,$fees);
        $shop->changIntegralMoney($shop_id, $shop_integral, $shop_money+$money);
    }
}